package com.cccyy.xml.xstream;

import com.cccyy.xml.vo.*;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

public class TestXStreamBack {

    public static void main(String[] args) {
        long time1 = System.currentTimeMillis();
        //测试java对象转xml，java对象中包含集合对象
        Service service = new Service();
        SysHeader sysHeader = new SysHeader();
        Data sysData = new Data();
        sysData.setName("SYS_HEAD");
        ArrayList<Data> sysDataList = new ArrayList<>();
        fillSysHeaderString(sysDataList, "SEQ_NO", "20180709180852");
        fillSysHeaderString(sysDataList, "TRAN_DATE", "20180720");

        Map sysMap = new HashMap<>();
        sysMap.put("RET_CODE", "000000");
        sysMap.put("RET_MSG", "Success");
        buildArrayBack("RET", sysData, sysDataList, sysMap);
        sysHeader.setData(sysData);
        service.setSysheader(sysHeader);

        AppHeader appHeader = new AppHeader();
        Data appData = new Data();
        appData.setName("APP_HEAD");
        Struct appStruct = new Struct();
        appData.setStruct(appStruct);
        appHeader.setData(appData);
        service.setAppheader(appHeader);

        LocalHeader localHeader = new LocalHeader();
        Data localData = new Data();
        localData.setName("LOCAL_HEAD");
        Struct localStruct = new Struct();
        localData.setStruct(localStruct);
        localHeader.setData(localData);
        service.setLocalheader(localHeader);

        Body body = new Body();

        ArrayList<Data> dataList = new ArrayList<>();
        fillSysHeaderString(dataList, "OP_CLIENT", "Y");
        fillSysHeaderString(dataList, "UPDATE_USER", "00020137");

        Map bodyMap = new HashMap<>();
        bodyMap.put("PREF_FLAG", "Y");
        bodyMap.put("POSTAL_CODE", "111111");

        ArrayList<Struct> structDataList1 = new ArrayList<>();

        Struct struct1 = new Struct();
        Data data1 = new Data();
        data1.setName("CONTACT_INFO");
        Array array = new Array();
        ArrayList<Data> arrayDataList1 = new ArrayList<>();
        Data data11 = new Data();
        data11.setName("PREF_FLAG");
        Field field1 = new Field();
        field1.setType("string");
        field1.setLength("1");
        field1.setText("Y");
        data11.setField(field1);
        arrayDataList1.add(data11);
        Data data12 = new Data();
        data12.setName("POSTAL_CODE");
        Field field2 = new Field();
        field2.setType("string");
        field2.setLength("6");
        field2.setText("111111");
        data12.setField(field2);
        arrayDataList1.add(data12);
        struct1.setStruct(arrayDataList1);

        Struct struct2 = new Struct();
        ArrayList<Data> arrayDataList2 = new ArrayList<>();
        Data data21 = new Data();
        data21.setName("PREF_FLAG");
        Field field3 = new Field();
        field3.setType("string");
        field3.setLength("1");
        field3.setText("N");
        data21.setField(field3);
        arrayDataList2.add(data21);
        Data data22 = new Data();
        data22.setName("POSTAL_CODE");
        Field field4 = new Field();
        field4.setType("string");
        field4.setLength("6");
        field4.setText("222222");
        data22.setField(field4);
        arrayDataList2.add(data22);
        struct2.setStruct(arrayDataList2);

        structDataList1.add(struct1);
        structDataList1.add(struct2);
        array.setStruct(structDataList1);
        data1.setArray(array);
        dataList.add(data1);


//        fillSysHeaderString(dataList, "GLOBAL_ID", "");
//        fillSysHeaderString(dataList, "ISS_COUNTRY", "");
        body.setData(dataList);
        service.setBody(body);
        String xml = XStreamUtil.beanToXml(service);

        String sle = String.valueOf(xml.length());
        StringBuffer le = new StringBuffer();
        if (8 > sle.length()) {
            for (int i = 0; i < 8 - sle.length(); i++) {
                le.append("0");
            }
            le.append(sle);
        }

        xml = le.append(xml).toString();
        System.out.println(xml);

        //测试xml转java对象
        xml = "00016596<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<service>\n" +
                "<sys-header>\n" +
                "<data name=\"SYS_HEAD\">\n" +
                "<struct>\n" +
                "<data name=\"SEQ_NO\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"14\">20180709180852</field>\n" +
                "</data>\n" +
                "<data name=\"TRAN_DATE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"8\">20180720</field>\n" +
                "</data>\n" +
                "<data name=\"RET\">\n" +
                "<array>\n" +
                "<struct>\n" +
                "<data name=\"RET_CODE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"6\">000000</field>\n" +
                "</data>\n" +
                "<data name=\"RET_MSG\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"7\">Success</field>\n" +
                "</data>\n" +
                "</struct>\n" +
                "</array>\n" +
                "</data>\n" +
                "</struct>\n" +
                "</data>\n" +
                "</sys-header>\n" +
                "<app-header>\n" +
                "<data name=\"APP_HEAD\">\n" +
                "<struct/>\n" +
                "</data>\n" +
                "</app-header>\n" +
                "<local-header>\n" +
                "<data name=\"LOCAL_HEAD\">\n" +
                "<struct/>\n" +
                "</data>\n" +
                "</local-header>\n" +
                "<body>\n" +
                "<data name=\"OP_CLIENT\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"1\">Y</field>\n" +
                "</data>\n" +
                "<data name=\"UPDATE_USER\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"8\">00020137</field>\n" +
                "</data>\n" +
                "<data name=\"CONTACT_INFO\">\n" +
                "<array>\n" +
                "<struct>\n" +
                "<data name=\"PREF_FLAG\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"1\">Y</field>\n" +
                "</data>\n" +
                "<data name=\"POSTAL_CODE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"6\">111111</field>\n" +
                "</data>\n" +
                "</struct>\n" +
                "<struct>\n" +
                "<data name=\"PREF_FLAG\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"1\">N</field>\n" +
                "</data>\n" +
                "<data name=\"POSTAL_CODE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"6\">111111</field>\n" +
                "</data>\n" +
                "</struct>\n" +
                "</array>\n" +
                "</data>\n" +
                "<data name=\"CLIENT_CITY\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"4\">xxxx</field>\n" +
                "</data>\n" +
                "<data name=\"DOCUMENT_INFO\">\n" +
                "<array>\n" +
                "<struct>\n" +
                "<data name=\"PREF_FLAG\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"1\">N</field>\n" +
                "</data>\n" +
                "<data name=\"ISS_DATE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"8\">20170520</field>\n" +
                "</data>\n" +
                "</struct>\n" +
                "<struct>\n" +
                "<data name=\"PREF_FLAG\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"1\">Y</field>\n" +
                "</data>\n" +
                "<data name=\"ISS_DATE\">\n" +
                "<field type=\"string\" scale=\"0\" length=\"8\">20180118</field>\n" +
                "</data>\n" +
                "</struct>\n" +
                "</array>\n" +
                "</data>\n" +
                "</body>\n" +
                "</service>";
        String substring = xml.substring(8);
        Service service2 = (Service) XStreamUtil.xmlToBean(substring);
        List<Data> sysArrayRetDataList = service2.getSysheader().getData().getStruct().getStruct();
        for (Data sysArrayRetData : sysArrayRetDataList) {
            if ("RET".equals(sysArrayRetData.getName())) {
                List<Struct> sysArrayRetStructList = sysArrayRetData.getArray().getStruct();
                for (Struct sysArrayRetStruct : sysArrayRetStructList) {
                    List<Data> sysRetDataList = sysArrayRetStruct.getStruct();
                    for (Data sysRetData : sysRetDataList) {
                        System.out.println(sysRetData.getName() + ":" + sysRetData.getField().getText());
                    }
                }
            } else {
                System.out.println(sysArrayRetData.getName() + ":" + sysArrayRetData.getField().getText());
            }
        }

        List<Data> bodyDataListArray = service2.getBody().getData();
        for (Data bodyArrayRetData : bodyDataListArray) {
            if (StringUtils.isEmpty(bodyArrayRetData.getField())) {
                System.out.println(bodyArrayRetData.getName());
                List<Struct> bodyArrayRetStructList = bodyArrayRetData.getArray().getStruct();
                for (Struct bodyArrayRetStruct : bodyArrayRetStructList) {
                    List<Data> bodyRetDataList = bodyArrayRetStruct.getStruct();
                    for (Data sysRetData : bodyRetDataList) {
                        if (StringUtils.isEmpty(sysRetData.getField().getText())) {
                            System.out.println(sysRetData.getName());
                        } else {
                            System.out.println(sysRetData.getName() + ":" + sysRetData.getField().getText());
                        }
                    }
                }
            } else {
                if (StringUtils.isEmpty(bodyArrayRetData.getField().getText())) {
                    System.out.println(bodyArrayRetData.getName());
                } else {
                    System.out.println(bodyArrayRetData.getName() + ":" + bodyArrayRetData.getField().getText());
                }
            }
        }

        long time2 = System.currentTimeMillis();
        System.out.println((time2 - time1) + "ms");
    }

    private static void buildArrayBack(String arrayName, Data arrayData, ArrayList<Data> dataArrayList, Map<Object, Object> map) {
        Struct structOut = new Struct();
        Data data = new Data();
        data.setName(arrayName);
        Array array = new Array();
        ArrayList<Data> arrayDataList = new ArrayList<>();
        ArrayList<Struct> structDataList = new ArrayList<>();
        Struct struct = new Struct();
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            fillSysHeaderString(arrayDataList, entry.getKey().toString(), entry.getValue().toString());
        }
        struct.setStruct(arrayDataList);
        structDataList.add(struct);
        array.setStruct(structDataList);
        data.setArray(array);
        dataArrayList.add(data);
        structOut.setStruct(dataArrayList);
        arrayData.setStruct(structOut);
    }


    private static String getFormatTime(Date date, String yyyymmdd) {
        SimpleDateFormat sf = new SimpleDateFormat(yyyymmdd);
        return sf.format(date).replaceAll(":", "");
    }

    private static void fillSysHeaderString(ArrayList<Data> dataList, String headerName, String text) {
        Data data = new Data();
        data.setName(headerName);
        Field field = new Field();
        field.setLength(String.valueOf(text.length()));
        field.setType("string");
        field.setText(text);
        data.setField(field);
        dataList.add(data);
    }
}
